<?php

namespace App\Http\Controllers;

use App\Models\FsUser;
use App\Models\FsUserTask;
use App\Models\Role;
use App\Models\Task;
use App\Models\User;
use App\Transformers\CashTransformer;
use App\Transformers\FsUserPhoneTransformer;
use App\Transformers\FsUserRemarkTransformer;
use App\Transformers\FsUserTaskTransformer;
use App\Transformers\FsUserTransformer;
use App\Transformers\ImTransformer;
use App\Transformers\PhoneRecordTransformer;
use App\Transformers\UserSimpleTransformer;
use App\Transformers\UserTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Excel;
use App\Exports\FsUserExport;
use Illuminate\Support\Facades\Auth;


class FsUserController extends Controller
{
    /**
     * @api {get} /api/fsuser?username=XXX&order=register_time_asc&page=1&limit=2include=fsUserPhones,fsUserRemarks,fsUserHasIm.level,fsUserTasks 客户列表
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {string} search  账号姓名代理微信等搜索
     * @apiParam {array} deposit_total  存款总数 [0,0]
     * @apiParam {array} deposit_max    最大存款 [0,0]
     * @apiParam {array} last_charge_time    最后登陆时间 [0,0]天数
     * @apiParam {array} register_time    注册时间 [0,0] 天数
     * @apiParam {array} deposit_max    活跃会员 [0,0] 天数
     * @apiParam {array} last_login_time    最后登陆天数 [0,0] 天数
     * @apiParam {array} withdrawal_total_desc    提款总额 [0,0]
     *
     *
     * @apiParam {String} end_register_time  注册结束时间
     * @apiParam {String} register_time_asc  注册日期排序
     * @apiParam {String} deposit_total_asc  存款总额排序
     * @apiParam {String} deposit_count_asc  存款次数排序
     * @apiParam {String} withdrawal_total_desc  提款总额排序
     * @apiParam {String} withdrawal_count_desc  提款次数排序
     * @apiParam {String} deposit_max_desc  最大存款排序
     * @apiParam {String} profit_desc  最大存款排序
     * @apiParam {String} balance_desc  余额排序
     * @apiParam {String} last_charge_time_desc  最后存款日期排序
     * @apiParam {String} last_login_time_desc  最后登陆日期排序
     * @apiParam {int} user_id  查看某用户维护的会员
     *
     * @apiParam {int} page   当前页数
     * @apiParam {int} limit  返回总条数
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "username": "大客户",
     *                   "agent": null,
     *                   "register_time": "2018-12-24 13:42:38",
     *                   "balance": "0.00",
     *                   "last_login_time": null,
     *                   "last_charge_time": null,
     *                   "last_refund_time": null,
     *                   "ip": null,
     *                   "status": null,
     *                   "lev": null,
     *                   "true_name": null,
     *                   "true_id": null,
     *                   "phone_num": null,
     *                   "qq": null,
     *                   "mail": null,
     *                   "province": null,
     *                   "city": null,
     *                   "bank_name": null,
     *                   "bank_num": null,
     *                   "remark": null,
     *                   "site_id": null,
     *                   "deposit_count": "0.00",
     *                   "deposit_max": "0.00",
     *                   "deposit_total": "0.00",
     *                   "withdrawal_count": "0.00",
     *                   "withdrawal_total": "0.00",
     *                   "multiple": "0.0000"
     *               }
     *           ],
     *               "meta": {
     *                   "pagination": {
     *                       "total": 1,
     *                       "count": 1,
     *                       "per_page": 10,
     *                       "current_page": 1,
     *                       "total_pages": 1,
     *                       "links": null
     *                   }
     *               }
     *       }
     */
    public function index(Request $request,FsUser $fsUser){
        $users = $fsUser->search($request);

        return $this->response->paginator($users, new FsUserTransformer());
    }

    /**
     * @api {get} /api/fsuser?username=XXX&order=register_time_asc&page=1&limit=2 当前登陆的客户列表
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {string} search  账号姓名代理微信等搜索
     * @apiParam {array} deposit_total  存款总数 [0,0]
     * @apiParam {array} deposit_max    最大存款 [0,0]
     * @apiParam {array} last_charge_time    最后登陆时间 [0,0]天数
     * @apiParam {array} register_time    注册时间 [0,0] 天数
     * @apiParam {array} deposit_max    活跃会员 [0,0] 天数
     * @apiParam {array} last_login_time    最后登陆天数 [0,0] 天数
     * @apiParam {array} withdrawal_total_desc    提款总额 [0,0]

     *
     *
     * @apiParam {String} end_register_time  注册结束时间
     * @apiParam {String} register_time_asc  注册日期排序
     * @apiParam {String} deposit_total_asc  存款总额排序
     * @apiParam {String} deposit_count_asc  存款次数排序
     * @apiParam {String} withdrawal_total_desc  提款总额排序
     * @apiParam {String} withdrawal_count_desc  提款次数排序
     * @apiParam {String} deposit_max_desc  最大存款排序
     * @apiParam {String} profit_desc  最大存款排序
     * @apiParam {String} balance_desc  余额排序
     * @apiParam {String} last_charge_time_desc  最后存款日期排序
     * @apiParam {String} last_login_time_desc  最后登陆日期排序
     * @apiParam {String} user_id  查看某用户维护的会员
     *
     * @apiParam {int} page   当前页数
     * @apiParam {int} limit  返回总条数
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "username": "大客户",
     *                   "agent": null,
     *                   "register_time": "2018-12-24 13:42:38",
     *                   "balance": "0.00",
     *                   "last_login_time": null,
     *                   "last_charge_time": null,
     *                   "last_refund_time": null,
     *                   "ip": null,
     *                   "status": null,
     *                   "lev": null,
     *                   "true_name": null,
     *                   "true_id": null,
     *                   "phone_num": null,
     *                   "qq": null,
     *                   "mail": null,
     *                   "province": null,
     *                   "city": null,
     *                   "bank_name": null,
     *                   "bank_num": null,
     *                   "remark": null,
     *                   "site_id": null,
     *                   "deposit_count": "0.00",
     *                   "deposit_max": "0.00",
     *                   "deposit_total": "0.00",
     *                   "withdrawal_count": "0.00",
     *                   "withdrawal_total": "0.00",
     *                   "multiple": "0.0000"
     *               }
     *           ],
     *               "meta": {
     *                   "pagination": {
     *                       "total": 1,
     *                       "count": 1,
     *                       "per_page": 10,
     *                       "current_page": 1,
     *                       "total_pages": 1,
     *                       "links": null
     *                   }
     *               }
     *       }
     */
    public function currentIndex(Request $request,FsUser $fsUser,Task $task){
        //获取这个用户任务下的所有会员
        $carry_user_id = Auth::id();
        $task_ids = $task->where('carry_user_id',$carry_user_id)->pluck('id')->toArray();
        $res = DB::table('fs_user_tasks')->whereIn('task_id',$task_ids)->pluck('fs_user_id')->toArray();
        $res = array_unique($res);
        $users = $fsUser->search($request,$res);
        return $this->response->paginator($users, new FsUserTransformer());
    }

      /**
       * @api {get} /api/export?token=access_token 导出
       * @apiGroup FsUser
       *
       * @apiHeader {String} Content-Type application/json [必填]
       *
       * @apiParam {String} username  用户名
       * @apiParam {String} true_name  姓名
       * @apiParam {datetime} start_login_time  登陆开始时间
       * @apiParam {datetime} end_login_time  登陆最后时间
       * @apiParam {String} agent  代理商
       * @apiParam {datetime} start_register_time  注册开始时间
       * @apiParam {String} end_register_time  注册结束时间
       * @apiParam {String} register_time_asc  注册日期排序
       * @apiParam {String} deposit_total_asc  存款总额排序
       * @apiParam {String} deposit_count_asc  存款次数排序
       * @apiParam {String} withdrawal_total_desc  提款总额排序
       * @apiParam {String} withdrawal_count_desc  提款次数排序
       * @apiParam {String} deposit_max_desc  最大存款排序
       * @apiParam {String} profit_desc  最大存款排序
       * @apiParam {String} balance_desc  余额排序
       * @apiParam {String} last_charge_time_desc  最后存款日期排序
       * @apiParam {String} last_login_time_desc  最后登陆日期排序
       *
       * @apiParam {int} page   当前页数
       * @apiParam {int} limit  返回总条数
       *
       * @apiVersion 1.0.0
       * @apiSuccessExample {json} Success-Response:
       *       HTTP/1.1 200 OK
       *
       **/
    public function export(Request $request, FsUserExport $fsUserExport)
    {
        return $fsUserExport->condition($request);
    }

    /**
     * @api {get} /api/fsuser/:id/im 查看会员微信列表
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员id(url传参)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *
     **/
    public function userImIndex(FsUser $fsUser){
        return $this->response->collection($fsUser->userIms, new ImTransformer());
    }

    /**
     * @api {get} /api/fsuser/:id/remark?limit=1&page=2 查看某会员下所有备注
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员id(url传参)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *           {
     *               "data": [
     *                   {
     *                       "id": 12,
     *                       "remark": "测试备注1233342432",
     *                       "user_id": 1,
     *                       "fs_user_id": 51,
     *                       "created_at": "2019-01-07 10:09:22",
     *                       "updated_at": "2019-01-07 14:05:54"
     *                   }
     *               ],
     *                       "meta": {
     *                           "pagination": {
     *                           "total": 4,
     *                           "count": 1,
     *                           "per_page": 1,
     *                           "current_page": 2,
     *                           "total_pages": 4,
     *                           "links": {
     *                           "previous": "http://dianyun2.com/api/fsuser/51/remark?page=1",
     *                           "next": "http://dianyun2.com/api/fsuser/51/remark?page=3"
     *                       }
     *                   }
     *               }
     *           }
     *
     *
     **/
    public function remark(FsUser $fsUser,Request $request){
        $data = $fsUser->fsUserRemarks()->orderByDesc('created_at')->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        return $this->response->paginator($data, new FsUserRemarkTransformer());
    }
    /**
     * @api {get} /api/fsuser/:id/record?limit=10&page=1&include=user 查看某会员下所有回访
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员id(url传参)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *           {
     *               "data": [
     *                   {
     *                       "id": 12,
     *                       "remark": "测试备注1233342432",
     *                       "user_id": 1,
     *                       "fs_user_id": 51,
     *                       "created_at": "2019-01-07 10:09:22",
     *                       "updated_at": "2019-01-07 14:05:54"
     *                   }
     *               ],
     *                       "meta": {
     *                           "pagination": {
     *                           "total": 4,
     *                           "count": 1,
     *                           "per_page": 1,
     *                           "current_page": 2,
     *                           "total_pages": 4,
     *                           "links": {
     *                           "previous": "http://dianyun2.com/api/fsuser/51/remark?page=1",
     *                           "next": "http://dianyun2.com/api/fsuser/51/remark?page=3"
     *                       }
     *                   }
     *               }
     *           }
     *
     *
     **/
    public function record(FsUser $fsUser,Request $request){
        $data = $fsUser->phoneTwPlRecords()->orderByDesc('created_at')->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        return $this->response->paginator($data, new PhoneRecordTransformer());
    }

    /**
     * @api {get} /api/fsuser/:id/cash?limit=10&page=1 查看某会员下所有交易
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员id(url传参)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *           {
     *               "data": [
     *                     {
     *                       "id": 1,
     *                       "mem_name": "用户名",
     *                       "act_result": "类别",
     *                       "act_result_str": "类别字符串",
     *                       "currency": "币别",
     *                       "mem_gold": "金额",
     *                       "min_sum": "小计",
     *                       "memo": "备注",
     *                       "key_date": "交易日期"
     *                       }
     *               ],
     *                       "meta": {
     *                           "pagination": {
     *                           "total": 4,
     *                           "count": 1,
     *                           "per_page": 1,
     *                           "current_page": 2,
     *                           "total_pages": 4,
     *                           "links": {
     *                           "previous": "http://dianyun2.com/api/fsuser/51/remark?page=1",
     *                           "next": "http://dianyun2.com/api/fsuser/51/remark?page=3"
     *                       }
     *                   }
     *               }
     *           }
     *
     *
     **/
    public function cash(FsUser $fsUser,Request $request){
        $data = $fsUser->cashs()->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        return $this->response->paginator($data, new CashTransformer());
    }

    /**
     * @api {get} /api/fsuser/:id/im?include=user 查看某会员下所有qq和微信
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员id(url传参)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *           {
     *               "data": [
     *               {
     *                   "id": 2,
     *                   "im": "wx",
     *                   "alias": "",
     *                   "user_id": 1,
     *                   "value": "11111222222222",
     *                   "status": 1,
     *                   "created_at": "2019-01-02 15:29:35",
     *                   "updated_at": "2019-01-08 15:51:01",
     *                       "user": {
     *                           "id": 1,
     *                           "name": "admin",
     *                            "section": "",
     *                            "branch": "",
     *                            "role": "",
     *                            "section_id": null,
     *                           "branch_id": null,
     *                           "role_id": null,
     *                           "status": 1,
     *                           "created_at": "",
     *                           "updated_at": "2019-01-18 14:38:13"
     *                       }
     *                   }
     *               ],
     *                       "meta": {
     *                           "pagination": {
     *                           "total": 4,
     *                           "count": 1,
     *                           "per_page": 1,
     *                           "current_page": 2,
     *                           "total_pages": 4,
     *                           "links": {
     *                           "previous": "http://dianyun2.com/api/fsuser/51/remark?page=1",
     *                           "next": "http://dianyun2.com/api/fsuser/51/remark?page=3"
     *                       }
     *                   }
     *               }
     *           }
     *
     *
     **/
    public function userIm(FsUser $fsUser,Request $request){
        $data = $fsUser->userIms()->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        return $this->response->paginator($data, new ImTransformer());
    }

    /**
     * @api {get} /api/fsuser/:id/users 查看某会员下维护的人员
     * @apiGroup FsUser
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id  会员id(url传参)
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *           {
     *               "id": 1,
     *               "name": "admin",
     *               "section": "管理部门",
     *               "branch": "",
     *               "role": "超级管理员",
     *               "section_id": 1,
     *               "branch_id": null,
     *               "role_id": 2,
     *               "ip": "192.168.2.128",
     *               "status": 1,
     *               "phone_site": "au",
     *               "phone_setting_id": 1,
     *               "phone_type": "plivo",
     *               "username": "",
     *               "password": "",
     *               "created_at": "",
     *               "updated_at": "2019-02-27 13:44:28"
     *           }
     *           ],
     *               "meta": {
     *                   "pagination": {
     *                   "total": 2,
     *                   "count": 1,
     *                   "per_page": 1,
     *                   "current_page": 1,
     *                   "total_pages": 2,
     *                   "links": {
     *                       "previous": null,
     *                       "next": "http://dianyun2.com/api/fsuser/51/user?page=2"
     *                   }
     *               }
     *           }
     *       }
     *
     *
     **/
    public function users(FsUser $fsUser,Request $request,Role $role){
        $data=  FsUserTask::where('fs_user_id',$fsUser->id)->pluck('carry_user_id')->toArray();
        $ids = [];
        if($data){
            $ids = array_unique($data);
        }
        $res = User::whereIn('id',$ids)->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        $role = $role->getIdAndNameToArray();
        return $this->response->paginator($res, new UserTransformer($role));
    }
}
